﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlideImage : UserControl, IEmbeddedControl
    {

        private GlideImageSettings _settings;

        public GlideImage()
        {
            InitializeComponent();
            _settings = new GlideImageSettings(this);
            this.Width = 32;
            this.Height = 32;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            lock (this)
            {
                e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(0, 0, Width, Height));
                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        public ISettings Settings
        {
            get { return _settings; }
        }

        #region Moving/Sizing

        private bool _sizeV, _sizeH, _move;
        private Point _sizeRef;

        private void GlideImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (_sizeH || _sizeV)
            {
                if (_sizeH)
                    this.Width += e.X - _sizeRef.X;
                if (_sizeV)
                    this.Height += e.Y - _sizeRef.Y;
                Invalidate();
            }
            else if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            else
            {
                if (e.X >= this.Width - 8)
                {
                    if (e.Y >= this.Height - 8)
                        this.Cursor = Cursors.SizeNWSE;
                    else
                        this.Cursor = Cursors.SizeWE;
                }
                else if (e.Y >= this.Height - 8)
                    this.Cursor = Cursors.SizeNS;
                else
                    this.Cursor = Cursors.Default;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlideImage_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= this.Width - 8)
            {
                if (e.Y >= this.Height - 8)
                {
                    _sizeH = true;
                    _sizeV = true;
                }
                else
                    _sizeH = true;
            }
            else if (e.Y >= this.Height - 8)
                _sizeV = true;
            else
            {
                _move = true;
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void GlideImage_MouseUp(object sender, MouseEventArgs e)
        {
            _sizeH = false;
            _sizeV = false;
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
